﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE.DO;
using VINETLIFE.BL;

namespace VINETLIFE.AdminCP
{
    public partial class ListProperties : System.Web.UI.Page
    {
        cmsProductCategoryDO objCate = new cmsProductCategoryDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["Pid"]))
            {
                int productCategoryID = int.Parse(Request.QueryString["Pid"]);

                objCate.ProductCategoryID = productCategoryID;
                if (!IsPostBack)
                {
                    Functions.ddlDatabinder(ddlParentCate, cmsProductCategoryDO.PRODUCTCATEGORYID_FIELD, cmsProductCategoryDO.PRODUCTCATEGORYTITLE_FIELD, new cmsProductCategoryBL().SelectAll());
                    try
                    {
                        ddlParentCate.SelectedValue = productCategoryID.ToString();
                    }
                    catch
                    {
                    }
                    if (productCategoryID > 0)
                        grvPropertyDataSource(productCategoryID);
                }
            }
            else
            {
                if (!IsPostBack)
                {
                    Functions.ddlDatabinder(ddlParentCate, cmsProductCategoryDO.PRODUCTCATEGORYID_FIELD, cmsProductCategoryDO.PRODUCTCATEGORYTITLE_FIELD, new cmsProductCategoryBL().SelectAll());
                }
            }
        }
        protected void ddlParentCate_SelectedIndexChanged(object sender, EventArgs e)
        {
            int productCategoryID = int.Parse(ddlParentCate.SelectedValue);
            //if (productCategoryID == 0)
            //    grvPropertyDataSource(productCategoryID);
            //else
            //     grvPropertyDataSource(productCategoryID);
            Response.Redirect("ListProperties.aspx?Pid=" + productCategoryID);
        }
        protected void grvPropertyDataSource(int productCategoryID)
        {
            grvProperties.DataSource = new cmsPropertiesBL().SelectByProductCategory(productCategoryID);
            grvProperties.DataBind();
        }
        protected void grvProperties_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvProperties.EditIndex = e.NewEditIndex;
            grvPropertyDataSource(int.Parse(Request.QueryString["Pid"]));
        }

        protected void grvProperties_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            cmsPropertiesDO objProperty = new cmsPropertiesDO();
            objProperty.PropertyID = Convert.ToInt32(((Label)grvProperties.Rows[e.RowIndex].Cells[0].FindControl("lblPropertyID")).Text);
            objProperty = new cmsPropertiesBL().Select(objProperty);
            objProperty.PropertyCode = ((TextBox)grvProperties.Rows[e.RowIndex].Cells[1].FindControl("txtPropertyCode")).Text.Trim();
            objProperty.PropertyName = ((TextBox)grvProperties.Rows[e.RowIndex].Cells[2].FindControl("txtPropertyName")).Text.Trim();
            objProperty.PropertyType = ((DropDownList)grvProperties.Rows[e.RowIndex].Cells[3].FindControl("ddlPropertyType")).SelectedValue;
            new cmsPropertiesBL().Update(objProperty);

            grvProperties.EditIndex = -1;
            grvPropertyDataSource(int.Parse(Request.QueryString["Pid"]));
        }

        protected void grvProperties_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvProperties.EditIndex = -1;
            grvPropertyDataSource(int.Parse(Request.QueryString["Pid"]));
        }
        protected void grvProperties_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("EmptyInsert"))
            {
                TextBox txtProductCode = (TextBox)grvProperties.Controls[0].Controls[0].FindControl("txtProductCode");
                TextBox txtProductTitle = (TextBox)grvProperties.Controls[0].Controls[0].FindControl("txtEmptyPropertyName");
                DropDownList ddlProductType = (DropDownList)grvProperties.Controls[0].Controls[0].FindControl("ddlEmpPropertyType");

                cmsPropertiesDO objProperty = new cmsPropertiesDO();
                objProperty.PropertyName = txtProductTitle.Text.Trim();
                objProperty.PropertyType = ddlProductType.SelectedValue;
                objProperty.ProductCategoryID = int.Parse(Request.QueryString["Pid"]);
                new cmsPropertiesBL().Insert(objProperty);
                Functions.Alert("Thêm thuộc tính thành công!", "ListProperties.aspx?Pid=" + objCate.ProductCategoryID.ToString());
                //  grvPropertyDataSource(int.Parse(Request.QueryString["Pid"]));
            }
            else if (e.CommandName.Equals("Cancel"))
            {
                grvProperties.ShowFooter = false;
                grvPropertyDataSource(int.Parse(Request.QueryString["Pid"]));
            }
            if (e.CommandName.Equals("Insert"))
            {
                TextBox txtProductCode = (TextBox)grvProperties.FooterRow.FindControl("txtFPropertyCode");
                TextBox txtProductTitle = (TextBox)grvProperties.FooterRow.FindControl("txtFPropertyName");
                DropDownList ddlProductType = (DropDownList)grvProperties.FooterRow.FindControl("ddlfPropertyType"); ;

                cmsPropertiesDO objProperty = new cmsPropertiesDO();
                objProperty.PropertyName = txtProductTitle.Text.Trim();
                objProperty.PropertyType = ddlProductType.SelectedValue;
                objProperty.ProductCategoryID = int.Parse(Request.QueryString["Pid"]);
                new cmsPropertiesBL().Insert(objProperty);
                Functions.Alert("Thêm thuộc tính thành công!", "ListProperties.aspx?Pid=" + objCate.ProductCategoryID.ToString());
            }
        }
        protected void AddNewRecord(object sender, EventArgs e)
        {
            grvProperties.ShowFooter = true;
            grvPropertyDataSource(int.Parse(Request.QueryString["Pid"]));
        }
        public string ProductCategoryTitle()
        {
            string s = "";
            if (!String.IsNullOrEmpty(Request.QueryString["Pid"]))
            {
                int pid = int.Parse(Request.QueryString["Pid"]);
                s = new cmsProductCategoryBL().Select(new cmsProductCategoryDO { ProductCategoryID = pid }).ProductCategoryTitle;
            }
            return s;
        }

        protected void grvProperties_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            new cmsPropertiesBL().Delete(new cmsPropertiesDO { PropertyID = Convert.ToInt32(grvProperties.DataKeys[e.RowIndex].Value) });
            Functions.Alert("Xóa thuộc tính thành công!", "ListProperties.aspx?Pid=" + objCate.ProductCategoryID.ToString());
        }
    }
}